package bpm.dao.impl.archive;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

import org.springframework.jdbc.core.RowMapper;

import bpm.domain.archive.Archive;
import bpm.domain.archive.ArchiveField;
import bpm.domain.archive.ArchiveItem;
import bpm.domain.document.Field;

/**
 * 
 * @author <a mailto="hwangsyin@gmail.com">King</a>
 *
 */
public class ArchiveItemMapper implements RowMapper {

	public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
		ArchiveItem item = new ArchiveItem();
		
		String archiveFieldKey = rs.getString("key");
		if(archiveFieldKey != null) {
			ArchiveField archiveField = new ArchiveField();
			
			archiveField.setId(rs.getInt("arc_field_id"));
			archiveField.setKey(archiveFieldKey);
			archiveField.setName(rs.getString("name"));
			archiveField.setDataType(rs.getString("data_type"));
			archiveField.setEditStyle(rs.getInt("edit_style"));
			archiveField.setEditArgument(rs.getString("edit_argument"));
			archiveField.setDisplayArgument(rs.getString("display_argument"));
			
			java.sql.Date createTime = rs.getDate("create_time");
			if(createTime != null) {
				archiveField.setCreateTime(new Date(createTime.getTime()));
			}
			
			archiveField.setDescription(rs.getString("description"));
			
			String documentFieldKey = rs.getString("doc_field_key");
			if(documentFieldKey != null) {
				Field field = new Field();
				field.setKey(documentFieldKey);
				
				archiveField.setDocumentField(field);
			}
			
			archiveField.setInputString(rs.getString("input"));
			archiveField.setDefaultValue(rs.getString("default_value"));
			archiveField.setParentXmlElement(rs.getString("parent_xml_element"));
			
			item.setField(archiveField);
		}
		
		item.setType(Archive.Type.TEXT);
		item.setValue(rs.getString("item_value"));
		return item;
	}

}
